package com.wushijia.intercepter;/**
 * Created by pc on 2017/8/14.
 */

import java.lang.reflect.Method;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

/**
 * 权限拦截器
 *
 * @author lee
 * @create 2017-08-14 11:35
 **/
public class AccessIntercepter extends HandlerInterceptorAdapter {

  //在调用方法之前执行拦截,或者对登录页面取消拦截
  @Override
  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    // 将handler强转为HandlerMethod, 前面已经证实这个handler就是HandlerMethod
    HandlerMethod handlerMethod = (HandlerMethod) handler;
    // 从方法处理器中获取出要调用的方法
    Method method = handlerMethod.getMethod();
    // 获取出方法上的Access注解
    /*Access access = method.getAnnotation(Access.class);
    if (access == null) {
        // 如果注解为null, 说明不需要拦截, 直接放过
        return true;
    }else {
        request.getRequestURL();
        System.out.println(request.getRequestURL());
    }*/
    if (request.getRequestURL().toString().contains("login")) {
      return true;
    }
    return false;
  }

}
